<template>
  <div class="container">
    <div class="container-content">
      <el-row :gutter="10">
        <el-col :span="4">
          <div class="card-container">
            <el-card :body-style="{ padding: '5px' }" shadow="never">
              <template #header>
                <span class="card-title">内容分类</span>
              </template>
              <div class="card-content-full">
                <el-tree
                  ref="docTreeRef"
                  accordion
                  :check-strictly="true"
                  class="custom-tree"
                  :data="treeOptions.dataList"
                  highlight-current
                  node-key="id"
                  :props="treeOptions.props"
                  :show-checkbox="false"
                  @node-click="handleNodeClick"
                />
              </div>
            </el-card>
          </div>
        </el-col>
        <el-col :span="20">
          <vab-query-form>
            <vab-query-form-top-panel>
              <el-form
                :inline="true"
                label-width="80px"
                :model="tableOptions.queryForm"
              >
                <el-form-item label="内容标题">
                  <el-input
                    v-model="tableOptions.queryForm.docName"
                    placeholder="标题"
                    style="width: 100px"
                  />
                </el-form-item>
                <el-form-item label="关键字">
                  <el-input
                    v-model="tableOptions.queryForm.keyWord"
                    placeholder="关键字"
                    style="width: 100px"
                  />
                </el-form-item>
                <el-form-item label="创建时间">
                  <el-date-picker
                    v-model="state.createTimeRange"
                    end-placeholder="结束日期"
                    range-separator="至"
                    start-placeholder="开始日期"
                    type="daterange"
                    value-format="YYYY-MM-DD"
                    @change="handleDateChange"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button
                    icon="refresh-left"
                    type="warning"
                    @click="handleResetQuery"
                  >
                    重置
                  </el-button>
                  <el-button
                    icon="Search"
                    type="primary"
                    @click="queryTableData"
                  >
                    查询
                  </el-button>
                </el-form-item>
              </el-form>
            </vab-query-form-top-panel>
            <vab-query-form-left-panel :span="24">
              <el-button
                v-auth="[`doc/xz`]"
                icon="circle-plus"
                type="primary"
                @click="handleDocAdd"
              >
                添加
              </el-button>
              <el-button
                v-auth="[`doc/sc`]"
                icon="remove"
                type="danger"
                @click="handleDocDelete"
              >
                批量删除
              </el-button>
            </vab-query-form-left-panel>
          </vab-query-form>
          <el-table
            ref="docTableRef"
            v-loading="tableOptions.loading"
            border
            stripe
            :data="tableOptions.dataList"
            :height="tableOptions.height"
            :cell-style="tableStyle.cellStyle"
            :header-cell-style="tableStyle.headerCellStyle"
            @sort-change="handleSortChange"
            @selection-change="handleSelectionChange"
            @row-click="handleRowClick"
          >
            <el-table-column
              align="center"
              show-overflow-tooltip
              type="selection"
              width="50"
            />
            <el-table-column label="序号" type="index" width="55">
              <template #default="{ $index }">
                {{ $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="类别"
              prop="docTypeName"
              width="120"
            />
            <el-table-column
              align="center"
              label="标题"
              prop="docName"
              width="120"
            >
              <template #default="{ row }">
                <el-link
                  :href="'./#/cmsdetail?type=doc&id=' + row.id"
                  target="_blank"
                  type="primary"
                >
                  {{ row.docName }}
                </el-link>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="关键字"
              prop="keyWord"
              width="170"
            />
            <el-table-column
              align="center"
              label="置顶"
              prop="totopDesc"
              width="60"
            />
            <el-table-column
              align="center"
              label="备注"
              prop="remark"
              show-overflow-tooltip
              width="140"
            />
            <el-table-column
              align="center"
              :formatter="dateFormat"
              label="创建时间"
              prop="createTime"
              width="120"
            />
            <el-table-column
              align="center"
              border
              fixed="right"
              label="操作"
              width="180"
            >
              <template #default="{ row }">
                <el-button
                  v-auth="[`doc/xg`]"
                  size="small"
                  type="warning"
                  @click="handleDocEdit(row)"
                >
                  编辑
                </el-button>
                <el-button
                  v-auth="[`doc/sc`]"
                  size="small"
                  type="danger"
                  @click="handleDocDelete(row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            background
            :current-page="tableOptions.pagination.current"
            :layout="tableOptions.pagination.layout"
            :page-size="tableOptions.pagination.size"
            :total="tableOptions.pagination.total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </el-col>
      </el-row>
      <doc-edit ref="docEditRef" width="80%" @confirm="queryTableData" />
    </div>
  </div>
</template>
<script setup name="Doc">
import { useMessage, useMessageBox } from "@/hooks/message";
import DocEdit from "./components/docEdit.vue";
import { queryDocTable, deleteDoc } from "@/api/admin/cms/doc/";
import { querySystemTreeNode } from "@/api/admin/system/systemtree";
import { dateFormat } from "@/utils/common";
import { useTable } from "@/hooks/table";
import { useTree } from "@/hooks/tree";

const docEditRef = ref(null);
const docTreeRef = ref(null);
const docTableRef = ref(null);
const state = reactive({
  createTimeRange: []
});

const tableOptions = reactive({
  queryForm: {
    docTypeCode: "ALL",
    keyWord: "",
    docName: "",
    createTimeBegin: "",
    createTimeEnd: ""
  },
  queryFunction: queryDocTable,
  onReseted: () => {
    state.createTimeRange = [];
  },
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, docTableRef);

const treeOptions = reactive({
  queryForm: { treeCode: "cms" },
  props: {
    children: "children",
    label: "text"
  },
  queryFunction: querySystemTreeNode
});

onMounted(() => {
  queryTreeData();
});

const { queryTreeData } = useTree(treeOptions);

const handleDateChange = val => {
  if (val && val.length > 0) {
    tableOptions.queryForm.createTimeBegin = val[0];
    tableOptions.queryForm.createTimeEnd = val[1];
  } else {
    tableOptions.queryForm.createTimeBegin = "";
    tableOptions.queryForm.createTimeEnd = "";
  }
};

const handleNodeClick = data => {
  if (data.nodeCode) {
    tableOptions.queryForm.docTypeCode = data.nodeCode;
  }
  queryTableData();
};

const handleDocDelete = row => {
  if (row && row.id) {
    useMessageBox()
      .confirm("删除的内容将无法恢复，是否继续")
      .then(async () => {
        const { msg } = await deleteDoc(row.id);
        useMessage().success(msg);
        await queryTableData();
      });
  } else {
    if (tableOptions.dataSelections.length > 0) {
      const ids = tableOptions.dataSelections.map(item => item.id).join();
      useMessageBox()
        .confirm("删除的内容将无法恢复，是否继续")
        .then(async () => {
          const { msg } = await deleteDoc(ids);
          useMessage().success(msg, "success");
          await queryTableData();
        });
    } else {
      useMessage().warning("未选中任何行");
    }
  }
};

const handleDocEdit = row => {
  if (row) {
    docEditRef.value.show(row);
  } else {
    useMessage().warning("未选中任何数据");
  }
};

const handleDocAdd = () => {
  docEditRef.value.show();
};
</script>

<style lang="scss" scoped></style>
